﻿@page
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.SignalR
@using Remotely.Server.Hubs
@attribute [IgnoreAntiforgeryToken]
@inject SignInManager<RemotelyUser> SignInManager
@inject IHubContext<CasterHub> CasterHubContext
@inject IHubContext<ViewerHub> ViewerHubContext
@functions {
    public async Task<IActionResult> OnPost()
    {
        if (SignInManager.IsSignedIn(User))
        {
            var activeSessions = CasterHub.SessionInfoList.Where(x => x.Value.RequesterUserName == HttpContext.User.Identity.Name);
            foreach (var session in activeSessions)
            {
                await CasterHubContext.Clients.Client(session.Value.CasterSocketID).SendAsync("Disconnect", "User logged out.");
                await ViewerHubContext.Clients.Client(session.Value.RequesterSocketID).SendAsync("ConnectionFailed");
            }

            await SignInManager.SignOutAsync();
        }

        return Redirect("~/");
    }
}
